<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8" />
    <link rel="stylesheet" href="/layui/css/layui.css"  media="all">
</head>
<body>
<div class="layui-container" style="margin-top: 50px;">

    <div class="layui-container" style="width: 500px;height: 330px;padding-top: 60px;">
        <form class="layui-form"  action="" method="post"  name="user">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名：</label>
                <div class="layui-inline">
                    <input type="text" id="id" name="id" lay-verify="id" autocomplete="off" placeholder="请输入用户名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码：</label>
                <div class="layui-inline">
                    <input type="password" id="password" name="password" lay-verify="password" placeholder="请输入密码" autocomplete="off" class="layui-input" value="123456">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">昵称：</label>
                <div class="layui-inline">
                    <input type="text" id="nickname"  name="nickname" lay-verify="required" placeholder="请输入昵称" autocomplete="off" class="layui-input" value="测试">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">单选框</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="男" title="男" checked="">
                    <input type="radio" name="sex" value="女" title="女">
<!--                    <input type="radio" name="sex" value="禁" title="禁用" disabled="">-->
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">联系电话：</label>
                <div class="layui-inline">
                    <input type="text" id="telephone" name="telephone" lay-verify="telephone" placeholder="请输入联系电话" autocomplete="off" class="layui-input" value="13510032773">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">地址：</label>
                <div class="layui-inline">
                    <input type="text" id="address" name="address" lay-verify="required" placeholder="请输入地址" autocomplete="off" class="layui-input" value="深圳">
                </div>
            </div>

            <input type="hidden" name="type" value="0">

            <div class="layui-upload">
                <label class="layui-form-label">上传头像</label>
                <div class="layui-upload-list">
                    <input type="file"  accept="image/jpg,image/png,image/jpeg"
                           name="image" id="image" >
                </div>
            </div>

            <div class="layui-form-item">
                <input type="button" class="layui-btn" lay-submit="" id="commit" style="margin-left: 160px;" value="提交"></input>
            </div>

        </form>
    </div>
</div>
<script th:src="@{/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/layui/layui.all.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/js/jquery-3.4.1.js}"></script>
<script>
    layui.use(['form', 'element','upload'], function(){
        var form = layui.form;
        var element = layui.element;
        var $ = layui.$;
        var upload = layui.upload;


            // ajax上传


});
</script>
<script>
    $(function(){

        $("#commit").click(function () {
            console.log("enter");
            var id = $("#id").val();
            var nickname = $("#nickname").val();
            var password = $("#password").val();
            var telephone = $("#telephone").val();
            var address = $("#address").val();
            var sex = $('input[type=radio][name=sex]:checked').val();
            // 获取图片
            var logoUrl = document.getElementById("image");
            var file = logoUrl.files[0];
            console.log(id);
            console.log(sex);

            var formdata=new FormData();
            formdata.append("image",file);
            formdata.append("id",id);
            formdata.append("nickname",nickname);
            formdata.append("password",password);
            formdata.append("sex",sex);
            formdata.append("telephone",telephone);
            var token = layui.data('token').data;
            $.ajax({
                url:"/user/save",//这里是请求的地址
                type: 'POST',
                data: formdata,
                dataType: "json",
                headers:{"token":token},
                processData: false, // 告诉jQuery不要去处理发送的数据
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success:function(result){
                    // window.location.href = "admin.html"
                }
                ,error:function(result){

                }
            });
        });
    });

</script>
</body>
</html>